package com.hoapital.outpbillwork.module.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoapital.outpbillwork.module.entity.*;
import com.hoapital.outpbillwork.module.mapper.*;
import com.hoapital.outpbillwork.module.service.OutpDoctorAssistantService;
import com.hospital.common.core.constants.AllFinalValueUtils;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.module.entity.query.CommonTips;
import com.hospital.common.core.module.entity.query.TipsItems;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * ÃÅÕïÒ½Éú¸¨Õï¼ÇÂ¼ 服务实现类
 * </p>
 *
 * @author lvyao
 * @since 2021-02-25
 */
@Service
public class OutpDoctorAssistantServiceImpl extends ServiceImpl<OutpDoctorAssistantMapper, OutpDoctorAssistant> implements OutpDoctorAssistantService {

    @Resource
    private OutpDoctorAssistantMapper outpDoctorAssistantMapper;

    @Resource
    private ClinicMasterMapper clinicMasterMapper;

    @Resource
    private OutpMedrecMapper outpMedrecMapper;

    @Resource
    private OutpBillItemsTempMapper outpBillItemsTempMapper;

    @Resource
    private OutpPrepayRcptTempMapper outpPrepayRcptTempMapper;

    @Resource
    private OrderVsBillnoMapper orderVsBillnoMapper;

    @Resource
    private OutpOrderDescTempMapper outpOrderDescTempMapper;

    @Resource
    private ExamAppointsMapper examAppointsMapper;

    @Resource
    private ExamItemsMapper examItemsMapper;

    @Resource
    private ExamBillItemsMapper examBillItemsMapper;

    @Resource
    private LabTestMasterMapper labTestMasterMapper;

    @Resource
    private LabTestItemsMapper labTestItemsMapper;

    @Resource
    private OutpDoctorPrescMasterMapper outpDoctorPrescMasterMapper;

    @Resource
    private OutpDoctorPrescItemsMapper outpDoctorPrescItemsMapper;

    @Override
    public String getApplyNo(String visitDate, String visitNo, String itemCode) {
        return outpDoctorAssistantMapper.getApplyNo(visitDate, visitNo, itemCode);
    }

    @Override
    public boolean updateAllTab(String type, ClinicMaster clinicMaster, OutpMedrec outpMedrec, List<OutpOrderDescTemp> outpOrderDescTempList, List<OutpBillItemsTemp> outpBillItemsTempList, List<OutpDoctorAssistant> outpDoctorAssistantList, List<Object> objectMasterList, List<Object> objectItemsList, List<Object> objectBillitemsList, OutpPrepayRcptTemp outpPrepayRcptTemp, List<OrderVsBillno> orderVsBillnoList) {

        if (ObjectUtil.isNotNull(clinicMaster)) {
            QueryWrapper<ClinicMaster> apply = new QueryWrapper<ClinicMaster>().apply("patient_id = {0}  and outp_id = {1}", clinicMaster.getPatientId(), clinicMaster.getOutpId());
            Integer integer = clinicMasterMapper.selectCount(apply);
            if (integer == 0) {
                integer = clinicMasterMapper.insert(clinicMaster);
            } else {
                integer = clinicMasterMapper.update(clinicMaster, apply);
            }
            if (!(integer >= 1)) {
                throw new ExceptionHelp(ExceptionHelpEnum.CLINIC_MASTER_INSERT);
            }
        }
        if (ObjectUtil.isNotNull(outpPrepayRcptTemp)) {
            Integer insertRec = outpPrepayRcptTempMapper.insert(outpPrepayRcptTemp);
            if (!(insertRec >= 1)) {
                throw new ExceptionHelp(ExceptionHelpEnum.OUTP_PREPAY_RCPT_TEMP_INSERT);
            }
        }
        if (ObjectUtil.isNotNull(outpMedrec)) {
            QueryWrapper<OutpMedrec> applyMedrec = new QueryWrapper<OutpMedrec>().apply("patient_id = {0}  and outp_id = {1}", clinicMaster.getPatientId(), clinicMaster.getOutpId());
            Integer insert1 = outpMedrecMapper.selectCount(applyMedrec);
            if (insert1 == 0) {
                insert1 = outpMedrecMapper.insert(outpMedrec);
            } else {
                insert1 = outpMedrecMapper.update(outpMedrec, applyMedrec);
            }
            if (!(insert1 >= 1)) {
                throw new ExceptionHelp(ExceptionHelpEnum.OUTP_MEDREC_INSERT);
            }
        }
        for (OutpOrderDescTemp outpOrderDescTemp : outpOrderDescTempList) {
            Integer insert2 = outpOrderDescTempMapper.insert(outpOrderDescTemp);
            if (!(insert2 >= 1)) {
                throw new ExceptionHelp(ExceptionHelpEnum.OUTP_ORDER_DESC_TEMP_INSERT);
            }
        }
        if (CollectionUtils.isNotEmpty(orderVsBillnoList)) {
            for (OrderVsBillno orderVsBillno : orderVsBillnoList) {
                Integer insert2 = orderVsBillnoMapper.insert(orderVsBillno);
                if (!(insert2 >= 1)) {
                    throw new ExceptionHelp(ExceptionHelpEnum.OUTP_PREPAY_RCPT_VS_BILL_INSERT);
                }
            }
        }
        for (OutpBillItemsTemp outpBillItemsTemp : outpBillItemsTempList) {
            Integer insert2 = outpBillItemsTempMapper.insert(outpBillItemsTemp);
            if (!(insert2 >= 1)) {
                throw new ExceptionHelp(ExceptionHelpEnum.OUTP_BILL_ITEMS_TEMP_INSERT);
            }
        }
        if (!type.equals(AllFinalValueUtils.WEST_DRUG)&&!type.equals(AllFinalValueUtils.EAST_DRUG)) {
            for (OutpDoctorAssistant outpDoctorAssistant : outpDoctorAssistantList) {
                Integer insert2 = outpDoctorAssistantMapper.insert(outpDoctorAssistant);
                if (!(insert2 >= 1)) {
                    throw new ExceptionHelp(ExceptionHelpEnum.OUTP_DOCTOR_ASSISTANT_INSERT);
                }
            }
        }
        switch (type){
            case AllFinalValueUtils.EXAM:
                for (Object object : objectMasterList) {
                    ExamAppoints examAppoints = (ExamAppoints) object;
                    Integer insert2 = examAppointsMapper.insert(examAppoints);
                    if (!(insert2 >= 1)) {
                        throw new ExceptionHelp(ExceptionHelpEnum.EXAM_MASTER_INSERT);
                    }
                }
                for (Object object : objectItemsList) {
                    ExamItems examItems = (ExamItems) object;
                    Integer insert2 = examItemsMapper.insert(examItems);
                    if (!(insert2 >= 1)) {
                        throw new ExceptionHelp(ExceptionHelpEnum.EXAM_ITEMS_INSERT);
                    }
                }
                for (Object object : objectBillitemsList) {
                    ExamBillItems examBillItems = (ExamBillItems) object;
                    Integer insert2 = examBillItemsMapper.insert(examBillItems);
                    if (!(insert2 >= 1)) {
                        throw new ExceptionHelp(ExceptionHelpEnum.EXAM_BILL_ITEMS_INSERT);
                    }
                }
                break;
            case AllFinalValueUtils.TEST:
                for (Object object : objectMasterList) {
                    LabTestMaster labTestMaster = (LabTestMaster) object;
                    Integer insert2 = labTestMasterMapper.insert(labTestMaster);
                    if (!(insert2 >= 1)) {
                        throw new ExceptionHelp(ExceptionHelpEnum.LAB_TEST_MASTER_INSERT);
                    }
                }
                for (Object object : objectItemsList) {
                    LabTestItems labTestItems = (LabTestItems) object;
                    Integer insert2 = labTestItemsMapper.insert(labTestItems);
                    if (!(insert2 >= 1)) {
                        throw new ExceptionHelp(ExceptionHelpEnum.LAB_TEST_ITEMS_INSERT);
                    }
                }
                break;
            case AllFinalValueUtils.EAST_DRUG:
                updatePresc(objectMasterList,objectItemsList);
                break;
            case AllFinalValueUtils.WEST_DRUG:
                updatePresc(objectMasterList,objectItemsList);
                break;
            default:
        }
        return true;
    }

    @Override
    public Object getGuideText(String orderedByDept, String performedBy, String itemCode) {
        CommonTips commonTips = outpDoctorPrescMasterMapper.getGuideText(orderedByDept, performedBy, itemCode);
        if (ObjectUtil.isNull(commonTips)){
            commonTips = outpDoctorPrescMasterMapper.getGuideText("*", "*", itemCode);
        }
        if (ObjectUtil.isNull(commonTips)){
            return null;
        }
        List<TipsItems> tipsItems = outpDoctorPrescMasterMapper.getAllTipsItems(commonTips.getGuideKeyCode(),commonTips.getGuideValueCode());
        commonTips.setTipsItemsList(tipsItems);
        return commonTips;
    }

    public void updatePresc(List<Object> objectMasterList, List<Object> objectItemsList){
        for (Object object : objectMasterList) {
            OutpDoctorPrescMaster outpDoctorPrescMaster = (OutpDoctorPrescMaster) object;
            Integer insert2 = outpDoctorPrescMasterMapper.insert(outpDoctorPrescMaster);
            if (!(insert2 >= 1)) {
                throw new ExceptionHelp(ExceptionHelpEnum.PRESC_MASTER_INSERT);
            }
        }
        for (Object object : objectItemsList) {
            OutpDoctorPrescItems outpDoctorPrescItems = (OutpDoctorPrescItems) object;
            Integer insert2 = outpDoctorPrescItemsMapper.insert(outpDoctorPrescItems);
            if (!(insert2 >= 1)) {
                throw new ExceptionHelp(ExceptionHelpEnum.PRESC_ITEMS_INSERT);
            }
        }
    }
}
